2016-06-30

Why interactive graphics?

Technique Related Questions Examples
Identification what is this point/mark? display info on hover
Zoom & pan is there local structure? click & drag to alter x/y limits
Categorical filter how do I navigate all these levels? click on legend entries
Linked highlighting how does the marginal/joint compare to a conditional? linked brushing on a scatterplot matrix

  • Why should presentation graphics be interactive?
    • Draw in your audience
    • Helps get your point across
  • Why should exploratory graphics be interactive?
    • Generate insight faster.

Why web graphics? Portability!

Why web graphics? Composability!

Say u do have time fo dat

  • First off, congratulations!
  • You can now create awesome interactive graphics!
  • Unfortunately, this skill-set is not practically useful for exploring data (where visualization type is unknown).

Identification, zoom & pan w/ ggplotly

library(plotly)
p <- ggplot(diamonds, aes(price, carat, color = cut)) + 
  geom_point(alpha = 0.05)
p %>% ggplotly() %>% toWebGL()

Extending James' plot

Linked highlighting via ggplotly

That's great, but…

  • ggplot2's interface wasn't designed for interactive graphics.
  • ggplot2 requires data frame(s) and can be inefficient (especially for time series).
  • plotly.js supports visualizations that ggplot2 doesn't.
  • plot_ly() provides a low-level R interface to plotly.js (now open source!).
  • Smarter defaults, informative warnings/errors, and higher-level functionality are on their way.

Smart defaults (also, no data frame necessary!)

plot_ly(z = ~volcano)
#> No trace type specified. Applying `add_heatmap()`.
#> Read more about this trace type here -> https://plot.ly/r/reference/#heatmap

Informative warnings/errors

plot_ly(puppies = rnorm(100)) %>% add_boxplot()
#> Error: Must supply either `x` or `y` attribute
plot_ly(x = rnorm(100)) %>% add_boxplot(name = "puppies")

plot_ly loves dplyr

txhousing %>%
  group_by(city) %>%
  plot_ly(x = ~date, y = ~median) %>%
  add_lines()

split-apply-________?

mpg %>%
  group_by(drv) %>%
  do(p = plot_ly(., x = ~cty, name = ~drv))
#> Source: local data frame [3 x 2]
#> Groups: <by row>
#> 
#>     drv                       p
#>   <chr>                  <list>
#> 1     4 <S3: plotly/htmlwidget>
#> 2     f <S3: plotly/htmlwidget>
#> 3     r <S3: plotly/htmlwidget>

split-apply-subplot!

mpg %>%
  group_by(drv) %>%
  do(p = plot_ly(., x = ~cty, name = ~drv)) %>%
  .[["p"]] %>% 
  subplot(nrows = NROW(.), shareX = TRUE)